package com.zhongjian.technologycal.mall.activity.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.LoginActivity;
import com.zhongjian.technologycal.mall.activity.main.bean.CourseHomeBean;
import com.zhongjian.technologycal.mall.activity.main.fragment.CourseDetailsCatalogFragment;
import com.zhongjian.technologycal.mall.activity.main.fragment.CourseDetailsTeacherFragment;
import com.zhongjian.technologycal.mall.activity.main.fragment.CourseDetailsbreifFragment;
import com.zhongjian.technologycal.mall.adapter.CommonViewPagerAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/14.
 * 课程详情
 */

public class CourseDetailsActivity extends BaseActivity {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.course_details_tab)
    TabLayout mTabLayout;
    @BindView(R.id.course_details_vp)
    ViewPager mViewPager;
    @BindView(R.id.course_details_iv_icon)
    ImageView course_details_iv_icon;
    @BindView(R.id.course_details_tv_title)
    TextView course_details_tv_title;
    @BindView(R.id.course_details_tv_price)
    TextView course_details_tv_price;
    @BindView(R.id.course_details_commit)
    TextView course_details_commit;
    private String titleName;
    private double meetingPrice;
    private String startTime;
    private String meetingAddress;
    private String meetingPhoto;


    private static int TAB_MARGIN_DIP = 40;
    private int tabStutusId;
    private String[] titles = {"简介", "讲师", "目录"};

    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("课程详情");
        tabStutusId = getIntent().getIntExtra("courseId", 0);
        initViewtRequest();

        CommonViewPagerAdapter infoPagerAdapter = new CommonViewPagerAdapter(getSupportFragmentManager(), titles);
        CourseDetailsbreifFragment fragment0 = CourseDetailsbreifFragment.newInstance(titles[0]);
        CourseDetailsTeacherFragment fragment1 = CourseDetailsTeacherFragment.newInstance(titles[1]);
        CourseDetailsCatalogFragment fragment2 = CourseDetailsCatalogFragment.newInstance(titles[2]);
        infoPagerAdapter.addFragment(fragment0);
        infoPagerAdapter.addFragment(fragment1);
        infoPagerAdapter.addFragment(fragment2);

        mViewPager.setAdapter(infoPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(3);
//        AppTools.setIndicator(this,mTabLayout,TAB_MARGIN_DIP,TAB_MARGIN_DIP);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_course_details;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    /**
     *
     */
    private void initViewtRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).getUrlRequestCoursedetailMain(tabStutusId, new Subscriber<ResponseBody>() {
            @Override
            public void onStart() {
                super.onStart();
                LoadingDialog.showDialogForLoading(CourseDetailsActivity.this);
            }

            @Override
            public void onCompleted() {
                LoadingDialog.cancelDialogForLoading();
            }

            @Override
            public void onError(Throwable e) {
                LoadingDialog.cancelDialogForLoading();
                ToastUitl.showShort(e.getMessage());
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                LoadingDialog.cancelDialogForLoading();
                try {
                    Gson gson = new Gson();
                    CourseHomeBean courseHomeBean = gson.fromJson(responseBody.string(), CourseHomeBean.class);
                    if (courseHomeBean.getError_code() == 0) {
                        course_details_tv_title.setText(courseHomeBean.getData().getCoursename());
                        course_details_tv_price.setText("¥" + courseHomeBean.getData().getCourseprice());
                        if (courseHomeBean.getData().getCoursePhotos().size() > 0) {

                            Picasso.get().load(BaseContacts.BASE_URL + courseHomeBean.getData().getCoursePhotos().get(0).getPhoto())
                                    .into(course_details_iv_icon);
                        } else {

                        }
                        titleName = courseHomeBean.getData().getCoursename();
                        startTime = courseHomeBean.getData().getCoursestarttime();
                        meetingPrice = courseHomeBean.getData().getCourseprice();
                        meetingAddress = courseHomeBean.getData().getCoursesite();
                        meetingPhoto = courseHomeBean.getData().getCoursePhotos().get(0).getPhoto();
                        if(courseHomeBean.getData().getCurstatus() == 0){
                            course_details_commit.setText("立即报名");
                            course_details_commit.setBackgroundResource(R.drawable.ripple_press_nomal);
                            course_details_commit.setClickable(true);
                        }else if(courseHomeBean.getData().getCurstatus() == 1){
                            course_details_commit.setText("授课中");
                            course_details_commit.setBackgroundColor(getResources().getColor(R.color.border_color));
                            course_details_commit.setClickable(false);
                        }
                    } else {
                        ToastUitl.showShort(courseHomeBean.getError_message());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    public String getCourseId() {
        return String.valueOf(tabStutusId);
    }

    /**
     * 提交报名信息
     */
    @OnClick(R.id.course_details_commit)
    public void onClickEnroll() {
        Intent intent = new Intent();
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            intent.setClass(getBaseContext(), LoginActivity.class);
            startActivity(intent);
        } else {
            intent.setClass(getBaseContext(), MineEnrollActivity.class);
            intent.putExtra("titleName", titleName);
            intent.putExtra("startTime", startTime);
            intent.putExtra("meetingPrice", meetingPrice);
            intent.putExtra("meetingAddress", meetingAddress);
            intent.putExtra("meetingPhoto", meetingPhoto);
            intent.putExtra("tabStutusId", tabStutusId);
            startActivity(intent);
        }


    }


}
